package com.findyou.controller;

import com.findyou.pojo.Pwd;
import com.findyou.pojo.Result;
import com.findyou.pojo.User;
import com.findyou.service.UserService;
import com.findyou.utils.JwtUtil;
import com.findyou.utils.Md5Util;
import com.findyou.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Title: UserController
 * @Author 贾金博
 * @Package com.findyou.controller
 * @Date 2024/3/9 10:38
 * @description:
 */

@RestController
@RequestMapping("user")
@Slf4j
@Validated // 参数校验的注解
@CrossOrigin // 跨域
public class UserController {

    @Autowired
    private UserService userService;


    //注意是大S
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        //TODO: 注册的时候 检测用户名是否已经被占用了 还有请求参数的校验
        if(username != null && username.length() >= 5 && username.length() <= 16 &&
            password != null && password.length() >= 5 && password.length() <= 16
        ) {
            boolean flag = userService.findByUsername(username);
            if(!flag) {
                return Result.error("该用户的名称被注册过了");
            }
            Result result = userService.register(username, password);
            //log.info("flag = {}", flag);  // 这里的 {} 是占位的
            return result;
        }
        return Result.error("参数不合法");
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        // 根据用户名字查询USer
        password = Md5Util.getMD5String(password); // 对比的是加密后的密码
        Result result = userService.findByUsername2(username, password);
        return result;
    }

    @GetMapping("/userInfo")
    public Result userInfo(@RequestHeader(name = "Authorization") String token) {
        //根据用户的id去获得全部信息
        Map<String, Object> map = JwtUtil.parseToken(token);
        int id = (int) map.get("id");
        Result result = userService.userInfo(id);
        return result;
    }

    @PutMapping("update")
    public Result update(@RequestBody User user) {

        //参数校验 加上@Validated
        System.out.println("user = " + user);
        Result result = userService.update(user);
        return result;
    }

    @PatchMapping("updateAvatar")
    public Result updateAvatar(@URL String avatarUrl, @RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        int id = (int) map.get("id");
        Result result = userService.updateAvatar(avatarUrl, id);
        return result;
    }

    @PatchMapping("updatePwd")
    public Result updatePwd(@RequestHeader(name = "Authorization") String token,
                            @RequestBody Pwd pwd) {
        Map<String, Object> map1 = ThreadLocalUtil.get(); // 这样参数里面的注解 就可以不用写了
        //TODO:用完之后要清除掉 防止内存泄露
        //TODO:其实可以不用一个对象 或者可以用Map<String, String>去接收数据 它会自动转换为Map
        //TODO:删除对应的redis
        String oldPwd = pwd.getOldPwd();
        String newPwd = pwd.getNewPwd();
        String rePwd = pwd.getRePwd();
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        Map<String, Object> map = JwtUtil.parseToken(token);
        int id = (int) map.get("id");
        Result result = userService.updatePwd(id, oldPwd, newPwd, rePwd);
        return result;
    }
}
